<script>
	import {
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	import {
		inputInviteCode,
		getIndexConfig
	} from '@/api/user'
	import {
		userShare,
		getConfig
	} from '@/api/app'
	import Cache from '@/utils/cache'
	import {
		strToParams,
		showModal,
		setTabbar
	} from '@/utils/tools'
	import {
		wxMnpLogin
	} from '@/utils/login'
	export default {
		globalData: {
			navHeight: ""
		},
		onLaunch: async function(options) {
			uni.hideTabBar({
				animation: false
			})
			// 获取配置
			this.getIndexConfigFun()
			// 获取分享
			this.getShareInfo()
			//获取系统信息
			this.getSystemInfo()

			this.UpdateManager()
			let {
				invite_code
			} = options.query
			console.log('-------options.query-------', options.query)
			if (invite_code) {
				Cache.set("user_sn", invite_code)
			}
			wxMnpLogin()
			// this.getUser()	
		},
		onShow: function(options) {},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// ...mapMutations(['SETCONFIG']),
			...mapActions(['getUser']),
			...mapGetters(['userInfo']),
			getSystemInfo() {
				uni.getSystemInfo({
					success: res => {
						let {
							statusBarHeight,
							platform
						} = res;
						let navHeight;

						if (platform == 'ios' || platform == 'devtools') {
							navHeight = statusBarHeight + 44;
						} else {
							navHeight = statusBarHeight + 48;
						}
						this.globalData.navHeight = navHeight;

					},
					fail(err) {
						console.log(err);
					}

				});
			},
			async getShareInfo() {
				const data = {
					mnp_share_title: '家用油标准，小锅现炒，食材新鲜',
					imageUrl: 'http://nsfadmin.womiwuyou.com/uploads/image/20231210/bc736bf7-b564-4d33-ba0b-1a8c87a66321.png'
					// imageUrl: 'http://yummy.xiangkekeji.cn/webImage/share-icon.png',
				}
				Cache.set('shareInfo', data)
				/* const {
					code,
					data
				} = await userShare()
				if (code == 1) {
					Cache.set('shareInfo', data)
				} */

			},
			async getConfigFun() {
				try {
					const {
						code,
						data
					} = await getConfig()
					// console.log('----------data', data)
					if (code == 1) {
						this.SETCONFIG(data)
						setTabbar()

					}
				} catch (e) {
					uni.showTabBar()
				}

			},
			bindCode(options) {
				let invite_code = options.query.invite_code
				if (invite_code) {
					Cache.set("user_sn", invite_code)
					/* inputInviteCode({
						code: invite_code
					}).then(res => {
						if (res.code == -1) {
							
						}
					}) */
				}
			},
			getIndexConfigFun() {
				getIndexConfig().then(({
					code,
					data
				}) => {
					Cache.set("_CONFIG", data)
				})
			},
			downLoadAndUpdate(updateManager) {
				updateManager.onUpdateReady(res => { // 新版本下载完成的回调
					updateManager.applyUpdate() // 强制当前小程序应用上新版本并重启
				})
				updateManager.onUpdateFailed(res => { // 新版本下载失败的回调
					// 新版本下载失败，提示用户删除后通过冷启动重新打开
					uni.showModal({
						content: '下载失败，请删除当前小程序后重新打开',
						showCancel: false,
						confirmText: '知道了'
					})
				})
			},
			UpdateManager() {
				const _this = this
				if (uni.canIUse('getUpdateManager')) {
					const updateManager = uni.getUpdateManager();
					updateManager.onCheckForUpdate(res => {
						if (res.hasUpdate) {
							wx.showModal({
								title: '更新提示',
								content: '检测到新版本，是否下载新版本并重启小程序？',
								success: function(res) {
									if (res.confirm) {
										//2. 用户确定下载更新小程序，小程序下载及更新静默进行
										_this.downLoadAndUpdate(updateManager)
									} else if (res.cancel) {
										//用户点击取消按钮的处理，如果需要强制更新，则给出二次弹窗，如果不需要，则这里的代码都可以删掉了
										wx.showModal({
											title: '温馨提示~',
											content: '本次版本更新涉及到新的功能添加，旧版本无法正常访问的哦~',
											showCancel: false, //隐藏取消按钮
											confirmText: "确定更新", //只保留确定更新按钮
											success: function(res) {
												if (res.confirm) {
													//下载新版本，并重新应用
													_this.downLoadAndUpdate(updateManager)
												}
											}
										})
									}
								}
							})
						}
					})
				} else {
					// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
					uni.showModal({
						title: '提示',
						content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
					})
				}
			},
		}
	}
</script>

<style lang='scss'>
	@import 'styles/base.scss';
	/*每个页面公共css */
	@import "components/uview-ui/index.scss";
</style>
